#include <QtGui>
#include "datedelegate.h"
#include "combodelegate.h"
#include "spindelegate.h"

enum Job{Worker,Farmer,Docter,Lawyer,Armyman};

int main( int argc, char **argv )
{
    QApplication app(argc, argv);
    
    QFont font("ZYSong18030",12);
    app.setFont(font);
    
    QTranslator translator;
    translator.load("delegate_zh");
    app.installTranslator(&translator);

    QStandardItemModel model(4, 4);
    QTableView tableView;
    tableView.setModel(&model);

    DateDelegate dateDelegate;
    ComboDelegate comboDelegate;
    SpinDelegate spinDelegate;
    tableView.setItemDelegateForColumn(1,&dateDelegate);
    tableView.setItemDelegateForColumn(2,&comboDelegate);
    tableView.setItemDelegateForColumn(3,&spinDelegate);
     
    model.setHeaderData(0,Qt::Horizontal,QObject::tr("Name"));
    model.setHeaderData(1,Qt::Horizontal,QObject::tr("Birthday"));
    model.setHeaderData(2,Qt::Horizontal,QObject::tr("Job"));
    model.setHeaderData(3,Qt::Horizontal,QObject::tr("Income"));
    
    QFile file("./data.tab");
    if (file.open(QFile::ReadOnly | QFile::Text)) 
    {
         QTextStream stream(&file);
         QString line;
         
         model.removeRows(0, model.rowCount(QModelIndex()), QModelIndex());

         int row = 0;
         do {
                 line = stream.readLine();
                 if (!line.isEmpty()) 
                 {

                     model.insertRows(row, 1, QModelIndex());

                     QStringList pieces = line.split(",", QString::SkipEmptyParts);
                     model.setData(model.index(row, 0, QModelIndex()),
                                    pieces.value(0));
                     model.setData(model.index(row, 1, QModelIndex()),
                                    pieces.value(1));
                     model.setData(model.index(row, 2, QModelIndex()),
                                    pieces.value(2));
                     model.setData(model.index(row,3, QModelIndex()),
                                    pieces.value(3));            
                     row++;
                 }
            } while (!line.isEmpty());

            file.close();
     }

     tableView.setWindowTitle(QObject::tr("Delegate"));
     tableView.show();
     return app.exec();
}
